package com.tianma315.admin.agent.rest;


import java.util.Arrays;

import com.tianma315.admin.agent.vo.AgentAddressVO;
import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.agent.domain.AgentAddressDO;
import com.tianma315.admin.agent.service.AgentAddressService;

/**
 * 
 * <pre>
 * 经销商地址
 * </pre>
 * <small> 2019-06-11 16:26:39 | Aron</small>
 */
@Controller
@RequestMapping("/agent/agentAddress")
public class AgentAddressController {
	@Autowired
	private AgentAddressService agentAddressService;
	
	@GetMapping()
	@RequiresPermissions("agent:agentAddress:agentAddress")
	String AgentAddress(){
	    return "agent/agentAddress/agentAddress";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("agent:agentAddress:agentAddress")
	public Result<Page<AgentAddressVO>> list(Integer pageNumber, Integer pageSize, AgentAddressVO agentAddressDTO){
		// 查询列表数据
        Page<AgentAddressVO> page = agentAddressService.agentAddressVOPage(pageNumber,pageSize,agentAddressDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("agent:agentAddress:add")
	String add(){
	    return "agent/agentAddress/add";
	}

	@GetMapping("/edit/{addressId}")
	@RequiresPermissions("agent:agentAddress:edit")
	String edit(@PathVariable("addressId") Long addressId,Model model){
		AgentAddressDO agentAddress = agentAddressService.selectById(addressId);
		model.addAttribute("agentAddress", agentAddress);
	    return "agent/agentAddress/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("agent:agentAddress:add")
	public Result<String> save( AgentAddressDO agentAddress){
		if(agentAddressService.addAgentAddress(agentAddress)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("agent:agentAddress:edit")
	public Result<String>  update( AgentAddressDO agentAddress){
		agentAddressService.updateById(agentAddress);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("agent:agentAddress:remove")
	public Result<String>  remove( Long addressId){
		if(agentAddressService.deleteById(addressId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("agent:agentAddress:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] addressIds){
		agentAddressService.deleteBatchIds(Arrays.asList(addressIds));
		return Result.ok();
	}
	
}
